﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CompactFramework.Utilities.Localization;

namespace TripLOAMobile
{

    public partial class ShowProfileInfo : LocalizedForm
    {
        WS_SoapLogin.ProfileInfo profile;
        Image avatar;
        bool current;
        private Bitmap m_DoubleBuffer; //Used to avoid flickering effects on paint procedure
        string parent;
        #region Initialization
        /// <summary>
        /// Initializes a new instance of the ShowProfileInfo class.
        /// </summary>
        public ShowProfileInfo()
        {
            #region DoubleBufferInitialization
            Size cSz = ClientSize;
            m_DoubleBuffer = new Bitmap(cSz.Width, cSz.Height);
            #endregion
            InitializeComponent();
            this.AddMenusToDictionary();
            UpdateResources();
        }

        /// <summary>
        /// Initializes a new instance of the ShowProfileInfo class.
        /// </summary>
        /// <param name="up">User Profile.</param>
        /// <param name="feedback">The numeric feedback.</param>
        /// <param name="isCurrent">if set to <c>true</c> it shows the profile of the logged user, else it shows the profile of another user</param>
        public ShowProfileInfo(WS_SoapLogin.ProfileInfo up, decimal feedback, bool isCurrent)
        {
            #region DoubleBufferInitialization
            Size cSz = ClientSize;
            m_DoubleBuffer = new Bitmap(cSz.Width, cSz.Height);
            #endregion
            InitializeComponent();
            current = isCurrent;
            profile = up;
            if (profile != null)
            {                
                avatar = (isCurrent) ? Welcome.rrp.GetCurrentAvatar() : Properties.Resources.unknownUser;
                pictureBox1.Image = avatar;
                fldNickname.Text = profile.Username;
                fldFeedback.Text = "Feedback: "+string.Format("( {0:F} )", feedback);
                fldName.Text = profile.Name;
                fldSurname.Text = profile.Surname;
                fldAddress.Text = profile.Address;
                fldAge.Text =(profile.Age<0)?"":profile.Age.ToString();
                fldBirthday.Text = (profile.Birthday.HasValue) ? "" + profile.Birthday.Value.ToShortDateString() : "";
                fldCity.Text = profile.City;
                fldEmail.Text = profile.Email;
                fldGender.Text = (profile.Gender.ToString()=="Unknown")?"":profile.Gender.ToString();
                fldLicense.Text = (profile.DrivingLicense.HasValue) ? 
                                        (profile.DrivingLicense.Value == true) ?
                                            ResourceManager.Instance.GetString("yes") : ResourceManager.Instance.GetString("no")
                                                : ResourceManager.Instance.GetString("no");
                fldPhoneNumber.Text = profile.PhoneNumber;
                switch (profile.Smoker)
                {
                    case TripLOAMobile.WS_SoapLogin.Smoker.NotSmoker: fldSmoker.Text = ResourceManager.Instance.GetString("no"); break;
                    case TripLOAMobile.WS_SoapLogin.Smoker.Smoker: fldSmoker.Text = ResourceManager.Instance.GetString("yes"); break;
                    //case TripLOAMobile.WS_SoapLogin.Smoker.Unknown: fldSmoker.Text = ResourceManager.Instance.GetString("unknown"); break;
                }
                fldInterests.Text = string.Join("\r\n", profile.Interests);
                if (profile.Interests.Length!=0) fldInterests.Height += 16 * (profile.Interests.Length-1);
            }
            if (Welcome.formBuffer.GetFormNames().Length == 0) menuItem2.Enabled = false;
            this.AddMenusToDictionary();
            UpdateResources();
        }

        /// <summary>
        /// The subclasses of the LocalizedForm should override this method and use this method to
        /// associate all menu items with custom names with the help of AddMenuToDictionary
        /// method.
        /// </summary>
        protected override void AddMenusToDictionary()
        {
            this.AddMenuToDictionary(this.menuLeft, "menuLeft");
            if (current) this.AddMenuToDictionary(this.menuRight, "menuRightInbox");
            else this.AddMenuToDictionary(this.menuRight, "menuRightSend");
            this.AddMenuToDictionary(this.menuItem1, "menuBack");
            this.AddMenuToDictionary(this.menuItem2, "menuHome");
            this.AddMenuToDictionary(this.menuItem3, "menuBackTo");
        }
        #endregion

        #region OnPainting
        /// <summary>
        /// Handles the PaintBackground event to avoid the background painting.
        /// </summary>
        /// <param name="e">The PaintEventArgs instance containing the event data.</param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        /// <summary>
        /// Handles the OnPainting event of the form
        /// </summary>
        /// <param name="e">A PaintEventArgs that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //Added to prevent painting of "ok" button
            WinAPI.ShowDoneButton(this.Handle, false);
            base.OnPaint(e);
            using (Graphics g = Graphics.FromImage(m_DoubleBuffer))
            {
                g.Clear(BackColor);
                g.DrawImage(Properties.Resources.circles, 3, AutoScrollPosition.Y + 3);
                Point[] p = new Point[]{
                    new Point(40,AutoScrollPosition.Y + 25), 
                    new Point(195,AutoScrollPosition.Y + 25), 
                    new Point(190,AutoScrollPosition.Y + 30), 
                    new Point(40,AutoScrollPosition.Y + 30)};
                using (Brush b = new SolidBrush(Color.Green))
                    g.FillPolygon(b, p);

                p[0].Y = p[1].Y = AutoScrollPosition.Y + 30;
                p[2].Y = p[3].Y = AutoScrollPosition.Y + 35;
                p[1].X -= 10;
                p[2].X -= 10;
                using (Brush b = new SolidBrush(Color.GreenYellow))
                    g.FillPolygon(b, p);
            }
            using (Graphics g = e.Graphics)
                g.DrawImage(m_DoubleBuffer, 0, 0);
        }
        #endregion

        #region Event Handling
        /// <summary>
        /// Handles the Click event of the fldFeedback control
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void fldFeedback_Click(object sender, EventArgs e)
        {
            ShowFeedbacks f = new ShowFeedbacks(profile.Feedbacks,avatar,profile.Username);
            Welcome.formBuffer.Add(f, ResourceManager.Instance.GetString("ShowFeedbacks"));
            f.ShowDialog();
            f.Dispose();
        }

        /// <summary>
        /// Handles the Click event of the menuRight control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuRight_Click(object sender, EventArgs e)
        {
            if (current)
            {
                ShowMessageBox inbox = new ShowMessageBox();
                Welcome.formBuffer.Add(inbox, ResourceManager.Instance.GetString("ShowMessageBox"));
                inbox.ShowDialog();
                inbox.Dispose();
            }
            else
            {
                if (profile != null)
                {
                    ShowSendMessage send = new ShowSendMessage(profile.Username);
                    Welcome.formBuffer.Add(send, ResourceManager.Instance.GetString("ShowSendMessage"));
                    send.ShowDialog();
                    send.Dispose();
                }
            }
        }
        #endregion

        /// <summary>
        /// Handles the Click event of the menuItem1(back) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem1_Click(object sender, EventArgs e)
        {        
            Welcome.formBuffer.Close();        
        }

        /// <summary>
        /// Handles the Click event of the menuItem2(back to home) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem2_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Clear();
        }

        /// <summary>
        /// Handles the Click event of the menuItem3(back to...) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem3_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.GoTo(parent);
        }

        /// <summary>
        /// Handles the Load event of the ShowProfileInfo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void ShowProfileInfo_Load(object sender, EventArgs e)
        {
            UpdateResources();
            string[] temp = Welcome.formBuffer.GetFormNames();
            switch (temp.Length)
            {
                case 1:
                    {
                        menuItem2.Enabled = false;
                        menuItem3.Enabled = false;
                        break;
                    }
                case 3:
                    {
                        menuItem3.Text += ResourceManager.Instance.GetString(temp[2]);
                        parent = temp[2];
                        break;
                    }
            }
        }
    }
}
